import sys
import os
import urllib3
from PySide6.QtWidgets import QApplication

from src.config import app_config
from src.utils.logger import setup_logger
from src.ui.theme import apply_theme, get_current_theme
from src.ui.i18n import translator # Import the instance
from src.ui.main_window import MainWindow


# Suppress only the InsecureRequestWarning from urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def main():
    """Main entry point for the application."""
    # --- 1. Create Qt Application ---
    app = QApplication(sys.argv)

    # --- 2. Initialize Translator ---
    # The translator instance is now a singleton, just load the language.
    translator.load_language(app_config.get('ui.language', 'en'))

    # --- 3. Apply Theme ---
    saved_theme = get_current_theme()
    apply_theme(saved_theme)

    # --- 4. Create and Show Main Window ---
    window = MainWindow(translator)
    window.show()
    # On macOS, it's sometimes necessary to explicitly raise and activate the window
    # to ensure it gets focus, especially when dealing with file dialogs later.
    window.raise_()
    window.activateWindow()

    # --- 5. Start Event Loop ---
    sys.exit(app.exec())

if __name__ == "__main__":
    main()